$(function() {
	
	//宾客进入弹框
	function guests_ComeIn(){
		//关闭弹框
		$("#tuiChu3 ").on("touchstart",function(){
			$(".guests_ComeIn").css("display","none");
		})
		//控制到入席状态
		$("#RuXi").on("touchstart",function(){
			$(".Table_web .table_head").css("display","none");
			$("#shengC").css("display","none");
			$(".Table_web .Number_guests").css("display","none");
			$(".Table_web .Guests_information").css("display","block");
			$(".guests_ComeIn").css("display","none");
			$(".Table_web .table_content").css("margin-top","20px");
		})
	}guests_ComeIn()
	
	//2.新增自己的婚宴      
		$.ajax({
			method: "post",
			url: "http://student.bluej.cn/index/wedding/add_wedding",
			async: true,
			data: {
				wechat_id: sessionStorage.openId,
				wedding_name: "溜得一匹 ",
				wedding_position: "广州",
				time: new Date(2017/11/11).getTime()
			},
			dataType: "jsonp",
			success: function(res) {
				console.log(res);
				//获取举办人的名字
				$(".Table_web .table_head h3").find("span").text(res.wedding_name);
				$(".guests_ComeIn p").find("span").text(res.wedding_name);
				$(".master_ComeIn p").find("span").text(res.wedding_name);
			}
		})
	
	//		3.获取婚宴座位 
	$.ajax({
		method: "post",
		url: "http://student.bluej.cn/index/wedding/get_wedding",
		async: true,
		data: {
			sponsor_id: 8,
		},
		dataType: "jsonp",
		success: function(res) {
			console.log(res)
			$(".guest .seat").find("img").attr("src",sessionStorage.headImg);
			$(".guests_ComeIn p").find("span").text(res.wedding_name);
			$(".master_ComeIn p").find("span").text(res.wedding_name);
			
			$(".banquet_main").on("touchend", ".seat", function() {
				if(guest_onoff == false) {
					if($(this).children().attr("src") == "img/vacancy.png") {
						$(this).children().attr("src", res.wedding_seats[0].head_img);
						$(".jzjw").css("display", "block");
						$(".jzjw .zhuohao").html($(this).parent().find(".number").html());
						$(".jzjw .weihao").html($(this).index())
					} else {
						$(".youren").css("display","block");
					}
				}
			})
			$(".youren .delete").on("touchstart",function(){
				$(".youren").css("display","none");
			})
			$(".youren .delete").on("touchend", function() {
				$(".youren").fadeOut(500);
			})
			$(".zhufu_mask .delete").on("touchend", function() {
				$(".zhufu_mask").fadeOut(500);
			})
		}
	})

	//4. 参加别人的婚宴    
	function Canjia() {
		$.ajax({
			method: "post",
			url: "http://student.bluej.cn/index/wedding/join_wedding",
			async: true,
			data: {
				wechat_id: sessionStorage.openId,
				sponsor_id: 8,
				seats: 0203,
				time: 23154684
			},
			dataType: "jsonp",
			success: function(res) {
				console.log(res);
			}
		})
	}
	Canjia()

	//5. 改变自己的座位     
	function GaiBian() {
		$.ajax({
			method: "post",
			url: "http://student.bluej.cn/index/wedding/change_seats",
			async: true,
			data: {
				wechat_id: sessionStorage.openId,
				sponsor_id: 8,
				seats: 0203,
			},
			dataType: "jsonp",
			success: function(res) {
				console.log(res);
			}
		})
	}
	GaiBian()

	//6. 添加六个位置  
	function TianJia() {
		$.ajax({
			method: "post",
			url: "http://student.bluej.cn/index/wedding/add_seats",
			async: true,
			data: {
				sponsor_id: 8,
			},
			dataType: "jsonp",
			success: function(res) {
				console.log(res);
			}
		})
	}
	TianJia()
})

function load(shengfen) {
	if(shengfen == "pengyou") {
		$(document).ready(function() {
			mask($(".json_mask"), $(".json_mask .mask_content"));
		})
	} else if(shengfen == "zhuren") {
		$(document).ready(function() {
			mask($(".json1_mask"), $(".json1_mask .mask_content"));
		})
	}
};
load();
var add_table = "<div class='morebito_table'><div class='table'><img src='img/table.png'/><div class='table_num'>第<span class='number'>1</span>席</div></div><div class='seat'><img src='img/vacancy.png' /></div><div class='seat'><img src='img/vacancy.png' /></div><div class='seat'><img src='img/vacancy.png' /></div><div class='seat'><img src='img/vacancy.png' /></div><div class='seat'><img src='img/vacancy.png' /></div><div class='seat'><img src='img/vacancy.png' /></div></div>";
var guests = 42;
var tbn = $(".number").length; //总桌数
var allseat = tbn * 6; //总的位置数
//动态获取数据添加桌子
(function() {
	if(guests.length > 36) {
		if(guests.length % 6 == 0) {
			var zuoshu = parseInt(guests.length / 6) - 6;
		} else {
			var zuoshu = parseInt(guests.length / 6) - 5;
		}
		for(var j = 0; j < zuoshu; j++) {
			$(".morebito_table:last-of-type").before(add_table);
			tbn++;
			tbn = $(".number").length;
			allseat = tbn * 6;
		}
	}
	$(".num_of_peaple").html(guests.length);
	$(".your_num").html(guests.length + 1);
	//席号
	$(".morebito_table .number").each(function(i) {
		$(this).html(i + 1);
	});
})();
//判断是否要添加桌子
(function() {
	$(".morebito").on("touchend", '.morebito_table:last-of-type', function() {
		if(tbn * 6 == guests.length) {
			tbn++;
			$(".morebito_table:last-of-type").before(add_table);
			$(".morebito_table .number").each(function(i) {
				$(this).html(i + 1);
			});
		} else {
			$(".kongwei").css("display","block");
		}
	})
	$(".kongwei .delete").on("touchstart",function(){
		$(".kongwei").css("display","none");
	})
})();
//主宾点击桌子放大缩小居中动画
(function() {
	var guest_onoff = true;
	var mydate = new Date();
	var bdhonoff = true;
	$(".guest .table").on("touchend", function() {
		if(new Date() - mydate > 600) {
			mydate = new Date();
			if(guest_onoff == true) {
				$(".morebito").fadeOut(100, function() {
					$(".guest").css({
						"-webkit-transform": "matrix(2,0,0,2,0,90)",
						'transition': "all 500ms"
					}); //transform的matrix矩阵（scaleX rotateY rotateX scaleY translateX translateY）
				});
				$(".pew").css("display", "none");
				$(".jzjw").css("display", "block");
				guest_onoff = false;

			} else if(guest_onoff == false) {
				$(".guest").css({
					"-webkit-transform": "matrix(1,0,0,1,0,0)",
					'transition': "all 500ms"
				});
				$(".morebito").fadeIn(100);
				guest_onoff = true;
				$(".pew").css("display", "block");
				$(".jzjw").css("display", "none");
			}
		}
	})
})();
var guest_onoff = true;
//各客宾桌动画
(function() {
	var mydate = new Date();

	$(".morebito").on("touchend", ".morebito_table:not(:last) .table", function() {
		var $left = $(this).offset().left;
		var $top = $(this).offset().top;
		var $D_width = $(document).width();
		var $D_height = $(document).height();
		var $width = $(this).width();
		var $height = $(this).height();
		var move_x = $D_width / 2 - $left - $width / 2;
		var move_y =$D_width, $D_height, $left, $top, $width, $height;
		if(new Date() - mydate > 600) {
			mydate = new Date();
			if(guest_onoff == true) {
				$(".center").css("overflow", "hidden");
				$(".guest").css({
					"transform": "scale(0)",
					"transition": " all 500ms",
					"opacity": "0"
				});
				$(this).parent(".morebito_table").siblings().fadeTo(100, 0, function() {
					$(this).css({
						scale: 0
					});
				});
				$(this).parent(".morebito_table").transition({
					scale: 3,
					x: ($D_width / 2 - $left - $width / 2) / 3 + "px",
					y: ($D_height / 2 - $top - $height / 2) / 3 + "px",
					//					scale:3
				}, 500)
				$(".pew").css("display", "none");
				guest_onoff = false;
			} else if(guest_onoff == false){
				$(".center").css("overflow", "overlay");
				$(".guest").css({
					"-webkit-transform": "matrix(1,0,0,1,0,0)",
					'transition': "all 500ms",
					"opacity": "1"
				});
				$(this).parent(".morebito_table").transition({
					scale: 1
				}, 200, function() {

					this.transition({
						x: "0px",
						y: "0px"
					}, 300, function() {

						this.siblings().fadeIn(100, function() {
							$(this).css({
								'scale': 1,
								"opacity": 1
							});
						})
					})
				})
				$(".pew").css("display", "block");
				$(".jzjw").css("display", "none");
				guest_onoff = true;
			}
		}
	})
})();


(function() {
	$(".json_mask .delete").on("touchend", function() {
		$(".json_mask").fadeOut(1000);
	})
})();
(function() {
	$(".json1_mask .delete").on("touchend", function() {
		$(".json1_mask").fadeOut(1000);
	})
})();